package cn.itcast.bos.action.take_delivery;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import cn.itcast.bos.utils.BaseAction;
public class WayBillAction extends BaseAction<WayBill> {
	//创建当前类的日志对象
	private static final Logger log = Logger.getLogger(WayBillAction.class);
	
	@Autowired
	private WayBillService wayBillService;
	@Action(value="waybill_save",results={@Result(name="success",type="json")})
	public String saveWayBill(){
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			if(model.getOrder()!=null && (model.getOrder().getId()==null || model.getOrder().getId()==0)){
				model.setOrder(null);
			}
			model.setSignStatus(1);//设置运单状态 1：代发货
			wayBillService.save(model);
			map.put("success", true);
			map.put("msg", "运单保存成功");
			log.info("运单保存成功,运单号："+model.getWayBillNum());
		}catch(Exception e){
			e.printStackTrace();
			map.put("success", false);
			map.put("msg", "运单保存失败");
			log.fatal("运单保存失败,运单号："+model.getWayBillNum());
		}
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	private Integer page;
	private Integer rows;
	public void setPage(Integer page) {
		this.page = page;
	}
	public void setRows(Integer rows) {
		this.rows = rows;
	}
	
	
	@Action(value="waybill_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		org.springframework.data.domain.Pageable pageable = new PageRequest(page-1, rows,new Sort(new Sort.Order(Sort.Direction.ASC, "id")) );
		Page<WayBill> page = wayBillService.findAll(model,pageable);
		pushPageDataToValueStack(page);
		return SUCCESS;
	}
	
	@Action(value="waybill_findByWayBillNum",results={@Result(name="success",type="json")})
	public String findByOrderNum(){
		WayBill wayBill = wayBillService.findByOrderNum(model.getWayBillNum());
		Map<String,Object> map = new HashMap<String,Object>();
		if(wayBill!=null){
			map.put("success", true);
			map.put("wayBillData", wayBill);
		}else{
			map.put("success", false);
		}
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
}
